package cn.weiyang.controller;

import cn.weiyang.model.Emp;
import cn.weiyang.model.Page;
import cn.weiyang.service.EmpService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/18.
 */
public class EmpAction extends ActionSupport implements ModelDriven<Emp> {
    /*注入用户业务服务*/
    @Autowired
    private EmpService empService;
    private Emp emp;
    private int pageSize;
    private int pageNum;
    private Page page;

    public EmpService getEmpService() {
        return empService;
    }

    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }

    public Emp getEmp() {
        return emp;
    }

    public void setEmp(Emp emp) {
        this.emp = emp;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }
    /**
     * 根据员工号查找员工
     * @return
     */
    public String findEmp(){
        Emp emp1=empService.findEmp(emp);
        ServletActionContext.getContext().put("data",emp1);
        /*Map<String,Object> request= (Map<String,Object>) ActionContext.getContext().get("request");*/
        ServletActionContext.getRequest().setAttribute("data",emp1);
        return SUCCESS;
    }
    /**
     * 模糊搜索员工信息
     * @return
     */
    public String getLikeEmp(){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("emp",emp);
        page=empService.getLikeEmp(map);
        return SUCCESS;
    }

    /**
     * 增加员工
     * @return
     */
    public String addEmp() {
        int res=empService.addEmp(emp);
        return SUCCESS;
    }

    /**
     * 修改员工信息
     * @return
     */
    public String updateEmp(){
        int res=empService.updateEmp(emp);
        return SUCCESS;
    }
    /**
     * 删除员工
     * @return
     */
    public String delEmp(){
        int res=empService.delEmp(emp);
        return SUCCESS;
    }
    public Emp getModel() {
        if(null==emp){
            emp=new Emp();
        }
        return emp;
    }
}
